package com.baike.controller;


import com.baike.data.Result;
import com.baike.em.ConstantEnum;
import com.baike.entity.TbDiscounts;
import com.baike.entity.TbStudentDiscounts;
import com.baike.mapper.TbDiscountsMapper;
import com.baike.mapper.TbStudentDiscountsMapper;
import com.baike.service.ITbStudentDiscountsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author libin
 * @since 2021-10-30
 */
@RestController
@RequestMapping("discountsController")
@RefreshScope//开启自动刷新
@Api(value = "优惠券服务")
public class TbDiscountsController {

    @Autowired
    private TbDiscountsMapper discountsMapper;
    @Autowired
    private ITbStudentDiscountsService iTbStudentDiscountsService;
    @Autowired
    private TbStudentDiscountsMapper studentDiscountsMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "discounts",value = "实体类",required = true,paramType = "body")
    })
    @PostMapping
    public Result add(@RequestBody TbDiscounts discounts){
        int i = discountsMapper.insert(discounts);
        return Result.build(ConstantEnum.SUCCESS);
    }


    @ApiOperation(value = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "购物券id",required = true,paramType = "query")
    })
    @DeleteMapping
    public Result delete(@RequestParam("id") Integer id){
        discountsMapper.deleteById(id);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "discounts",value = "实体类",required = true,paramType = "body"),
    })
    @GetMapping
    public Result update(@RequestBody TbDiscounts discounts){
        discountsMapper.updateById(discounts);
        return Result.build(ConstantEnum.SUCCESS);
    }


    @ApiOperation(value = "查询所有优惠券")
    @GetMapping("findAll")
    public Result findAll(){
        List<TbDiscounts> list = discountsMapper.selectList(null);
        return Result.build(ConstantEnum.SUCCESS).setResult(list);
    }



    @ApiOperation(value = "优惠券秒杀")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentId",value ="学生id",required = true,paramType = "query"),
            @ApiImplicitParam(name = "discountsId",value ="优惠券id",required = true,paramType = "query"),
    })
    @PostMapping("studentDiscounts")
    public Result studentDiscounts(Integer studentId){

        //查询该用户有没有抢购过优惠券,
        Boolean hasKey = redisTemplate.boundHashOps("studentDiscounts").hasKey(studentId);
        //如果没抢过,就让用户抢
        if (!hasKey){
            //先判断redis里 有没有优惠券  当有优惠券数量大于0 时  说明有  才能发放
            Integer num = (Integer) redisTemplate.boundValueOps("discountsNum").get();
            if (num > 0 ){
                redisTemplate.boundValueOps("discountsNum").decrement();
                //查询优惠券的类型总量
                List<TbDiscounts> list = discountsMapper.selectList(null);
                //获取优惠券类型列表的长度
                int size = list.size();
                //随机
                Random r = new Random();
                int i = r.nextInt(size);
                //tbDiscounts 随机的优惠券
                TbDiscounts tbDiscounts = list.get(i);
                //discountsId  优惠券的ID
                Integer discountsId = tbDiscounts.getDiscountsId();
                // 根据studentId  discountsId  查询数据库有没有这个优惠券
                TbStudentDiscounts studentDiscounts = iTbStudentDiscountsService.selectDiscounts(studentId,discountsId);
                //如果为空  就说明用户没抢过
                if (studentDiscounts == null){
                    //没抢过购物券,增加一张购物券
                    iTbStudentDiscountsService.insertDiscounts(studentId,discountsId);
                }else if (studentDiscounts != null){
                    //如果不为空,说明有该优惠券, 所以要加1
                    iTbStudentDiscountsService.updateStudentDiscountsCount(discountsId, studentDiscounts.getCount() + 1, studentId);
                }
                //put到redis中,做个标记,表示用户已抢购过
                redisTemplate.boundHashOps("studentDiscounts").put(studentId,1);
            }
        }
        return Result.build(ConstantEnum.SUCCESS);
    }


}
